/**
 * Created by admin on 16/2/21.
 */
//分析文档
'use strict';

const keyMap = {
    //keyword : weight
    'The austerity of love': 20,

    'The propensity of love': 5,
    'The depravity of love': 5,

    'with next door for me': 5,
    'making things cure to me': 5,
    'whenever time': 5,
    'got me means': 5,
    'got me love': 5,
    'realize what i got': 5
};

const keyReg = (() => {
    let obj = {};
    for (let key of Object.keys(keyMap)) {
        obj[key] = new RegExp(key, 'i');
    }
    return obj;
})();

let analysis = (doc, html) => {

    //补充标题
    if (doc.title == '') {
        try {
            doc.title = html.match(/<title>([\s\S]+?)<\/title>/i)[1];
        } catch (e) {
            doc.title = '';
        }
    }

    let matchingIndex = 1;
    let matchList = [];

    for (let key in keyMap) {
        if (keyMap.hasOwnProperty(key) && html.match(keyReg[key]) != null) {
            matchingIndex += keyMap[key];
            matchList.push(key);
        } else {
            //noop
        }
    }

    doc.status = matchingIndex;
    doc.weight = matchList.join('#');

    return doc;
};

module.exports = analysis;
